package com.kmproject.haifu.mvp.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.hfinterface.SecondHanSelectInterface;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.ui.activity.SecondHandInfoShowActivity;
import com.kmproject.haifu.mvp.ui.widget.CircleImageView;
import com.kmproject.haifu.mvp.ui.widget.RoundCornerImageView;

import java.util.List;

public class SecondHandMainShowAdapter extends BaseQuickAdapter<SecondHandInfoBean, BaseViewHolder> {
    ImageLoader glideImage;
    SecondHanSelectInterface secondHanSelectInterface;
    private boolean isShowCheck = false;//选择开关
    private int isselectAll = 0;//0表示既不是选择全部，也不是全部不选，1表示全选中  2表示全不选
    public SecondHandMainShowAdapter( @Nullable List<SecondHandInfoBean> data) {
        super(R.layout.second_hand_show_item, data);
    }

    public SecondHanSelectInterface getSecondHanSelectInterface() {
        return secondHanSelectInterface;
    }

    public void setSecondHanSelectInterface(SecondHanSelectInterface secondHanSelectInterface) {
        this.secondHanSelectInterface = secondHanSelectInterface;
    }

    public void setShowCheck(boolean showCheck) {
        isShowCheck = showCheck;
    }



    public void setIsselectAll(int isselectAll) {
        this.isselectAll = isselectAll;
    }

    @Override
    protected void convert(BaseViewHolder helper, SecondHandInfoBean item) {
        if (isShowCheck) {
            if (isselectAll == 1) {
                helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setChecked(true);
                item.setHaveSelect(true);
            } else if (isselectAll == 2) {
                helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setChecked(false);
                item.setHaveSelect(false);
            }else {
                helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setChecked(item.isHaveSelect());
            }
            helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setVisibility(View.VISIBLE);
            helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    item.setHaveSelect(isChecked);
                 if (secondHanSelectInterface!=null)
                 {
                     secondHanSelectInterface.clikItem(item,helper.getAdapterPosition());
                 }
                }
            });
        } else {
            helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setVisibility(View.GONE);
            item.setHaveSelect(false);
        }

      if (item.getIsShow()==0||item.getIsAvailable()==0)
      {
          //会员过期和已下架的都是未展示
          helper.<TextView>getView(R.id.mystore_head_renthouse_item_wzs_txt).setVisibility(View.VISIBLE);
          helper.<TextView>getView(R.id.second_hand_show_item_qx).setTextColor(ArmsUtils.getColor(mContext,R.color.white));
          helper.<TextView>getView(R.id.second_hand_show_item_qx).setBackgroundResource(R.drawable.button_trsant_bg);
      }else {
          helper.<TextView>getView(R.id.mystore_head_renthouse_item_wzs_txt).setVisibility(View.GONE);
          helper.<TextView>getView(R.id.second_hand_show_item_qx).setTextColor(ArmsUtils.getColor(mContext,R.color.color_191919));
          helper.<TextView>getView(R.id.second_hand_show_item_qx).setBackgroundResource(R.drawable.button_yellow_bg);
      }


        if (glideImage == null) {
            glideImage = ArmsUtils.obtainAppComponentFromContext(mContext).imageLoader();
        }
        if (item.getImages()!=null&&!item.getImages().isEmpty())
        {
            glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getImages().get(0).getImageUrl())
                    .imageView(helper.<RoundCornerImageView>getView(R.id.second_hand_show_item_RoundCornerImageView))
                    .build());
            helper.<RoundCornerImageView>getView(R.id.second_hand_show_item_RoundCornerImageView).setmBorderRadius(10);
            helper.<RoundCornerImageView>getView(R.id.second_hand_show_item_RoundCornerImageView).setmCornerType(RoundCornerImageView.CornerType.TOP);
        }

        String temLabName="";
        if (item.getLabels()!=null&&!item.getLabels().isEmpty())
        {
            for (SecondHandInfoBean.LabelsBean labelsBean:item.getLabels())
            {
                if (labelsBean.getName().contains("全新"))
                {
                    temLabName=labelsBean.getName();
                    break;
                }
            }
        }
        if (!TextUtils.isEmpty(temLabName))
        {
            helper.<TextView>getView(R.id.second_hand_show_item_qx).setVisibility(View.VISIBLE);
            helper.<TextView>getView(R.id.second_hand_show_item_descript).setText("\u3000\u3000"+(item.getTitle()==null?"":item.getTitle()));
        }else {
            helper.<TextView>getView(R.id.second_hand_show_item_qx).setVisibility(View.GONE);
            if (item.getTitle()==null||TextUtils.isEmpty(item.getTitle()))
            {
                helper.<TextView>getView(R.id.second_hand_show_item_descript).setVisibility(View.GONE);
            }else {
                helper.<TextView>getView(R.id.second_hand_show_item_descript).setVisibility(View.VISIBLE);
            }
            helper.<TextView>getView(R.id.second_hand_show_item_descript).setText(item.getTitle()==null?"":item.getTitle());
        }
        if (item.getTitle()==null)
        {
            helper.<TextView>getView(R.id.second_hand_show_item_descript).setVisibility(View.GONE);
        }
         helper.<TextView>getView(R.id.second_hand_show_item_money).setText(item.getSalePrice());
        if (item.getAreaInfo()!=null&&!item.getAreaInfo().isEmpty())
        {
            helper.<TextView>getView(R.id.second_hand_show_item_address).setText(item.getAreaInfo().get(item.getAreaInfo().size()-1).getName());
        }
       if (item.getPublishWay()==1)
       {
           //关联了店铺
           helper.<ConstraintLayout>getView(R.id.second_hand_show_item_userInfo_lay).setVisibility(View.GONE);
           helper.<ConstraintLayout>getView(R.id.second_hand_show_item_spInfo_lay).setVisibility(View.VISIBLE);
           if (TextUtils.isEmpty(item.getStoreInfo().getStoreLogo()))
           {
               helper.<RoundCornerImageView>getView(R.id.second_hand_show_item_spInfo_header_img).setBackgroundResource(R.drawable.default_headimage);
           }else {
               glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getStoreInfo().getStoreLogo())
                       .imageView(helper.<RoundCornerImageView>getView(R.id.second_hand_show_item_spInfo_header_img))
                       .build());
           }

           helper.<TextView>getView(R.id.second_hand_show_item_spInfo_name).setText(item.getStoreInfo().getStoreName());
           helper.<TextView>getView(R.id.second_hand_show_item_spInfo_hydj).setText(item.getStoreInfo().getStoreRankName());

       }else {
           //用户
           helper.<ConstraintLayout>getView(R.id.second_hand_show_item_userInfo_lay).setVisibility(View.VISIBLE);
           helper.<ConstraintLayout>getView(R.id.second_hand_show_item_spInfo_lay).setVisibility(View.GONE);
           if (TextUtils.isEmpty(item.getUserInfo().getAvatar()))
           {
               helper.<CircleImageView>getView(R.id.second_hand_show_item_userInfo_header_img).setBackgroundResource(R.drawable.default_headimage);
           }else {
               glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getUserInfo().getAvatar())
                       .imageView(helper.<CircleImageView>getView(R.id.second_hand_show_item_userInfo_header_img))
                       .build());
           }

           helper.<TextView>getView(R.id.second_hand_show_item_userInfo_name).setText(item.getUserInfo().getNickName());
           glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getUserInfo().getLevelHeadImage())
                   .imageView(helper.<ImageView>getView(R.id.second_hand_show_item_userInfo_hydj))
                   .build());
       }
       helper.itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent(mContext, SecondHandInfoShowActivity.class);
               UserBean userBean = DataHelper.getDeviceData(mContext, AppConstant.USER_INFO);//获取用户信息
             if (!userBean.getUserId().equals(item.getUserInfo().getUserId()))
             {
                 if (item.getIsShow()==0||item.getIsAvailable()==0)
                 {
                     //过去的和已下架的不能点，不是自己的
                     return;
                 }

             }else {
               intent.putExtra("type","userCenter");//可以编辑那些操作
             }
               intent.putExtra("secondHandId",item.getSecondHandId());
               ArmsUtils.startActivity(intent);
           }
       });
    }
}
